﻿using System.Web.UI.WebControls;

namespace CLFramework.Web.Utility
{
    /// <summary>
    /// 
    /// </summary>
    public static class WebUtility
    {
        #region FillDropDownList

        #region void FillDropDownList(DropDownList,object,string,string)

        /// <summary>
        /// 填充DropDownList，填充之后会选中列表中的第一项。
        /// </summary>
        /// <param name="dropDownList">要填充的DropDownList。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <remarks>此方法在绑定数据之前会清除列表项，
        ///如果不在绑定数据之前清除列表项请将先将DropDownList
        ///的AppendDataBoundItems属性设定为true。</remarks>
        public static void FillDropDownList(DropDownList dropDownList,
                                            object dataSource, string textField, string valueField)
        {
            if (Checker.IsNotEmpty(dataSource))
            {
                dropDownList.DataSource = dataSource;
                dropDownList.DataTextField = textField;
                dropDownList.DataValueField = valueField;
                dropDownList.DataBind();
            }
            else
            {
                dropDownList.Items.Clear();
            }
        }

        #endregion

        #region void FillDropDownList(DropDownList,object,string,string,string)

        /// <summary>
        /// 填充DropDownList，指定默认的选中值。
        /// </summary>
        /// <param name="dropDownList">要填充的DropDownList。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="defaultSeletedValue">默认的选中值。</param>
        public static void FillDropDownList(DropDownList dropDownList,
                                            object dataSource, string textField, string valueField,
                                            string defaultSeletedValue)
        {
            FillDropDownList(dropDownList, dataSource, textField, valueField);
            SelectDropDownListItem(dropDownList, defaultSeletedValue);
        }

        #endregion

        #region void FillDropDownList(DropDownList,object,string,string,ListItem)

        /// <summary>
        /// 填充DropDownList，并且在位置0插入默认项，填充之后将选中默认项。
        /// </summary>
        /// <param name="dropDownList">要填充的DropDownList。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="insertDefaultItem">要插入的默认项。</param>
        public static void FillDropDownList(DropDownList dropDownList,
                                            object dataSource, string textField, string valueField,
                                            ListItem insertDefaultItem)
        {
            FillDropDownList(dropDownList, dataSource, textField, valueField);
            if (!dropDownList.Items.Contains(insertDefaultItem))
            {
                dropDownList.Items.Insert(0, insertDefaultItem);
            }
        }

        #endregion

        #region void FillDropDownList(DropDownList,object,string,string,ListItem,string)

        /// <summary>
        /// 填充DropDownList，并且在位置0插入默认项，指定选中值。
        /// </summary>
        /// <param name="dropDownList">要填充的DropDownList。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="insertDefaultItem">要插入的默认项。</param>
        /// <param name="defaultSeletedValue">默认的选中值。</param>
        public static void FillDropDownList(DropDownList dropDownList,
                                            object dataSource, string textField, string valueField,
                                            ListItem insertDefaultItem, string defaultSeletedValue)
        {
            FillDropDownList(dropDownList, dataSource, textField, valueField, insertDefaultItem);
            SelectDropDownListItem(dropDownList, defaultSeletedValue);
        }

        #endregion

        #endregion

        #region FillListControl

        #region void FillListControl(ListControl,object,string,string)

        /// <summary>
        /// 填充ListControl，填充之后会选中列表中的第一项。
        /// </summary>
        /// <param name="ListControl">要填充的ListControl。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <remarks>此方法在绑定数据之前会清除列表项，
        ///如果不在绑定数据之前清除列表项请将先将ListControl
        ///的AppendDataBoundItems属性设定为true。</remarks>
        public static void FillListControl(ListControl ListControl,
                                           object dataSource, string textField, string valueField)
        {
            if (Checker.IsNotEmpty(dataSource))
            {
                ListControl.DataSource = dataSource;
                ListControl.DataTextField = textField;
                ListControl.DataValueField = valueField;
                ListControl.DataBind();
            }
            else
            {
                ListControl.Items.Clear();
            }
        }

        #endregion

        #region void FillListControl(ListControl,object,string,string,string)

        /// <summary>
        /// 填充ListControl，指定默认的选中值。
        /// </summary>
        /// <param name="ListControl">要填充的ListControl。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="defaultSeletedValue">默认的选中值。</param>
        public static void FillListControl(ListControl ListControl,
                                           object dataSource, string textField, string valueField,
                                           string defaultSeletedValue)
        {
            FillListControl(ListControl, dataSource, textField, valueField);
            SelectListControlItem(ListControl, defaultSeletedValue);
        }

        #endregion

        #region void FillListControl(ListControl,object,string,string,ListItem)

        /// <summary>
        /// 填充ListControl，并且在位置0插入默认项，填充之后将选中默认项。
        /// </summary>
        /// <param name="ListControl">要填充的ListControl。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="insertDefaultItem">要插入的默认项。</param>
        public static void FillListControl(ListControl ListControl,
                                           object dataSource, string textField, string valueField,
                                           ListItem insertDefaultItem)
        {
            FillListControl(ListControl, dataSource, textField, valueField);
            if (!ListControl.Items.Contains(insertDefaultItem))
            {
                ListControl.Items.Insert(0, insertDefaultItem);
            }
        }

        #endregion

        #region void FillListControl(ListControl,object,string,string,ListItem,string)

        /// <summary>
        /// 填充ListControl，并且在位置0插入默认项，指定选中值。
        /// </summary>
        /// <param name="ListControl">要填充的ListControl。</param>
        /// <param name="dataSource">要填充的数据源。</param>
        /// <param name="textField">显示域。</param>
        /// <param name="valueField">值域。</param>
        /// <param name="insertDefaultItem">要插入的默认项。</param>
        /// <param name="defaultSeletedValue">默认的选中值。</param>
        public static void FillListControl(ListControl ListControl,
                                           object dataSource, string textField, string valueField,
                                           ListItem insertDefaultItem, string defaultSeletedValue)
        {
            FillListControl(ListControl, dataSource, textField, valueField, insertDefaultItem);
            SelectListControlItem(ListControl, defaultSeletedValue);
        }

        #endregion

        #endregion

        #region SelectDropDownListItem

        /// <summary>
        /// 选中DropDownList中的项目
        /// </summary>
        /// <param name="dropDownList">DropDownList实例.</param>
        /// <param name="value">要选中的值</param>
        public static void SelectDropDownListItem(DropDownList dropDownList, string value)
        {
            ListItem listItem = dropDownList.Items.FindByValue(value);
            if (listItem != null)
            {
                dropDownList.ClearSelection();
                listItem.Selected = true;
            }
        }

        #endregion

        #region SelectListControlItem

        /// <summary>
        /// 选中ListControl中的项目
        /// </summary>
        /// <param name="ListControl">ListControl实例.</param>
        /// <param name="value">要选中的值</param>
        public static void SelectListControlItem(ListControl ListControl, string value)
        {
            ListItem listItem = ListControl.Items.FindByValue(value);
            if (listItem != null)
            {
                ListControl.ClearSelection();
                listItem.Selected = true;
            }
        }

        #endregion

    }
}
